﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CvModel;
using DAL;

namespace LAB3_Cv
{
    public partial class JobsApplied : System.Web.UI.Page
    {
        private Applicant _currentUser;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["CurrentUser"] != null)
                {
                    _currentUser = Session["CurrentUser"] as Applicant;
                    LoadAllAppliedAds();
                }
                
            }
        }

        private void LoadAllAppliedAds()
        {
            var allAppliedAds = WorkAdDB.GetAllAdsFromApplicantId(_currentUser.ApplicantId);
            if (allAppliedAds.Count < 1)
            {
                LabelNoAds.Visible = true;
                LabelAds.Visible = false;
            }
            ListViewAds.DataSource = allAppliedAds;
            ListViewAds.DataBind();
        }

        protected void ListViewAds_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            try
            {
                var listViewItem = e.Item as ListViewItem;

                var articleId = (int)ListViewAds.DataKeys[listViewItem.DataItemIndex].Value;
                Session["WorkAdId"] = articleId;
                Response.Redirect("WorkAd.aspx");
            }
            catch (Exception ex)
            {
                LabelException.Text = ex.Message;
            }
        }
        
    }
}